<?php

class AjaxController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {

    }

	function indexAction()
	{
		echo "Ajax";die;
	}
	
	//return geo info
	function checkTaobaoIdAction()
	{
		$params = $this->_request->getParams();
		$result = array('result' => 0);
		
		if($params['product_url'])
		{
			$mod_ext = new Sys_Extensions();
			$product_url_info = $mod_ext->FormatStandartTaobaoUrl(trim($params['product_url']));
			
			if($product_url_info['taobao_id'])
			{
				$mod_products = new Db_Tables_BjmProducts();
				if($mod_products->ProductExist($product_url_info['taobao_id']))
				{
					$result = array('result' => 1); //商品已存在
				}
			}else{
				$result = array('result' => 2); //链接有误
			}
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function productPlusAction()
	{
		$params = $this->_request->getParams();
		$result = array('result' => 0);
		
		$product_id = intval($params['product_id']);
		$product_price = floatval($params['product_price']);
		
		if($product_id)
		{
			$_SESSION['cart']['cart_amount'] += $product_price;
			$_SESSION['cart']['cart_qty'] += 1;
			
			if($_SESSION['cart']['product_ids'][$product_id])
			{
				$_SESSION['cart']['product_ids'][$product_id] += 1;
			}else{
				$_SESSION['cart']['product_ids'][$product_id] = 1;
			}
			
			$result = array('result' => 1);
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function productMinusAction()
	{
		$params = $this->_request->getParams();
		$result = array('result' => 0);
		
		$product_id = intval($params['product_id']);
		$product_price = floatval($params['product_price']);
		
		if($product_id)
		{
			if($_SESSION['cart']['product_ids'][$product_id] > 0)
			{
				$_SESSION['cart']['product_ids'][$product_id] -= 1;
				
				$_SESSION['cart']['cart_amount'] -= $product_price;
				if(0 > $_SESSION['cart']['cart_amount'])
				{
					$_SESSION['cart']['cart_amount'] = 0;
				}

				$_SESSION['cart']['cart_qty'] -= 1;
				if(0 > $_SESSION['cart']['cart_qty'])
				{
					$_SESSION['cart']['cart_qty'] = 0;
				}
			}else{
				unset($_SESSION['cart']['product_ids'][$product_id]);
			}
			
			if($_SESSION['cart']['product_ids'][$product_id] < 1)
			{
				unset($_SESSION['cart']['product_ids'][$product_id]);
			}
			
			$result = array('result' => 1);
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function getAroundBuildingsAction()
	{
		$params = $this->_request->getParams();
		$result = array('result' => 0);
		
		$mod_ext = new Sys_Extensions();
		$result = $mod_ext->GetAround($params['geo']);
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function getCitiesAction()
	{
		$params = $this->_request->getParams();
		
		$result = array();
		
		if($params['province_id'])
		{
			$mod_cities = new Db_Tables_ParamsAllCities();
			$mod_cities->province_id = $params['province_id'];
			$result = $mod_cities->MakeOptions();
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function getCountiesAction()
	{
		$params = $this->_request->getParams();
		
		$result = array();
		
		if($params['city_id'])
		{
			$mod_counties = new Db_Tables_ParamsAllCounties();
			$mod_counties->city_id = $params['city_id'];
			$result = $mod_counties->MakeOptions();
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function getMateriasAction()
	{
		$params = $this->_request->getParams();
		
		$result = array();
		
		if($params['cmval'])
		{
			$mod_materias = new Db_Tables_Materias();
			$mod_materias->cmid = $params['cmval'];
			$result = $mod_materias->GetItems();
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function saveCartInSessionAction()
	{
		$params = $this->_request->getParams();
		
		$_SESSION['cart']['temp'] = array();
		
		if($params['comments'])
		{
			$_SESSION['cart']['temp']['comments'] = $params['comments'];
		}
		
		if($params['invoice'])
		{
			$_SESSION['cart']['temp']['invoice'] = $params['invoice'];
		}
		
		$result = array('result' => 1);
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function cellphoneAvailableAction()
	{
		$params = $this->_request->getParams();
		$result = array(
			'result' => 0,
			'reason' => "未知错误"
		);
		
		if($params['cellphone'])
		{
			$cellphone = trim($params['cellphone']);
		}else{
			$cellphone = NULL;
		}
		
		if($_SESSION['identity']['member_id'] && $cellphone)
		{
			$mod_members = new Db_Tables_Members();
			$row = $mod_members->fetchRow("cellphone='".$cellphone."' and member_id != ".$_SESSION['identity']['member_id']);
			
			if(!$row)
			{
				$result = 1;
				
				$mod_ext = new Sys_Extensions();
				$result = $mod_ext->SendVcode($cellphone);
			}else{
				$result = array(
					'result' => 0,
					'reason' => "手机号已被其他会员使用，请更换手机号"
				);
			}
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function adminCellphoneAvailableAction()
	{
		$params = $this->_request->getParams();
		$result = array(
			'result' => 0,
			'reason' => "未知错误"
		);
		
		if($params['cellphone'])
		{
			$cellphone = trim($params['cellphone']);
		}else{
			$cellphone = NULL;
		}
		
		if($cellphone)
		{
			if($params['admin_id'])
			{
				$mod_admins = new Db_Tables_Admins();
				$row = $mod_admins->fetchRow("cellphone='".$cellphone."' and admin_id != ".intval($params['admin_id']));
			}else{
				$mod_admins = new Db_Tables_Admins();
				$row = $mod_admins->fetchRow("cellphone='".$cellphone."'");
			}
			
			if($row)
			{
				$result = array(
					'result' => 0,
					'reason' => "手机号已被其他管理员占用，请更换手机号"
				);
			}else{
				$mod_vip = new Db_Tables_MembersVip();
				$row = $mod_vip->fetchRow("vip_phone='".$cellphone."' and vip_type='C'");
				
				if($row)
				{
					$result = array(
						'result' => 0,
						'reason' => "手机号已被店长占用，请更换手机号"
					);
				}else{
					$result = array(
						'result' => 1,
						'reason' => "手机号可用"
					);
				}
			}
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	/**
	 * @deprecated 验证验证码
	 * @return json [description]
	 */
	function checkVcodeAction()
	{
		$params = $this->_request->getParams();
    	
    	$result = 0;
		
		if($params['cellphone'] && $params['vcode'])
		{
			$mod_cellphone_veri = new Db_Tables_CellphoneVerification();
			$result = $mod_cellphone_veri->CheckVcode($params['cellphone'], $params['vcode']);
		}
		
		$json = array("result" => $result);
    	
    	echo Zend_Json::encode($json); //back to ajax
    	die;
	}
	
	function checkNewOrdersAction()
	{
		$params = $this->_request->getParams();
		
		$result = 0;
		
		if($params['current_id'])
		{
			$mod_orders = new Db_Tables_Orders();
			$row = $mod_orders->fetchRow("order_id>".intval($params['current_id'])." and status=1");
			
			if($row)
			{
				$result = 1;
			}
		}
		
		$json = array("result" => $result);
		
		echo Zend_Json::encode($json); //back to ajax
    	die;
	}
	
	function orderInfoForPrintAction()
	{
		$params = $this->_request->getParams();
		
		$order_info_for_print = array();
		
		if($params['order_id'])
		{
			$mod_orders = new Db_Tables_Orders();
			$row = $mod_orders->fetchRow("order_id=".intval($params['order_id']));
			if($row)
			{
				$row->status = 2;
				$row->save();
			}
			
			$mod_order_joins = new Db_Joins_OrdersJoins();
			$mod_order_joins->order_id = $params['order_id'];
			$order_info = $mod_order_joins->SingleOrderInfoForLabel();
			
			$mod_ext = new Sys_Extensions();
			$order_info_for_print = $mod_ext->SingleLabelDecolation($order_info);
		}
		
		$json = $order_info_for_print;
		
		echo Zend_Json::encode($json); //back to ajax
    	die;
	}
	
	function checkPcEventStatusAction()
	{
		$params = $this->_request->getParams();
		
		$member_id_array = array();
		$members_info = array();
		$result = array();
		
		if($params['event_id'])
		{
			$mod_params = new Db_Tables_Params();
			$event_start_time = $mod_params->GetVal("PC_EVENT_".intval($params['event_id']));
			
			if($event_start_time)
			{
				$select = $this->db->select();
				$select->from("logs-pc-events as e", array("member_id"));
				$select->joinLeft("members as m", "m.member_id=e.member_id", array("member_name", "headimgurl"));
				$select->where("e.event_id = ?", intval($params['event_id']));
				$rows = $this->db->fetchAll($select);
				
				if($rows)
				{
					foreach($rows as $row)
					{
						$member_id_array[] = $row['member_id'];
						$members_info[$row['member_id']] = array(
							'member_name' => $row['member_name'],
							'headimgurl' => $row['headimgurl']
						);
					}
					
					if(!empty($member_id_array))
					{
						$select = $this->db->select();
						$select->from("logs-subscribe", array("member_id", "count(id) as ct"));
						$select->where("ctime >= ?", $event_start_time);
						$select->where("member_id IN (?)", $member_id_array);
						$select->group("member_id");
						$select->order("ct DESC");
						$select->limit(10);
						$rows = $this->db->fetchAll($select);
						
						if($rows)
						{
							$ct_array = array();
							
							foreach($rows as $row)
							{
								$members_info[$row['member_id']]['ct'] = $row['ct'];
								$ct_array[$row['member_id']] = $row['ct'];
							}
							
							if(!empty($ct_array))
							{
								arsort($ct_array);
								
								foreach($ct_array as $member_id => $ct)
								{
									$result[] = $members_info[$member_id];
								}
							}
						}
					}
				}
			}
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function cleanEventDataAction()
	{
		$params = $this->_request->getParams();
		
		if($params['event_id'])
		{
			$mod_logs_pc_events = new Db_Tables_LogsPcEvents();
			$mod_logs_pc_events->event_id = $params['event_id'];
			$mod_logs_pc_events->CleanEventData();
		}
		
		die;
	}
	
	function memberInfoAction()
	{
		$params = $this->_request->getParams();
		
		$result = array(
			"result" => 0,
			"member_id" => "",
			"member_name" => "",
			"headimgurl" => "",
			"balance_due" => "",
			"agent_balance" => "",
			"vip_balance" => "",
			"delivery_balance" => ""
		);
		
		if($params['cellphone'])
		{
			$select = $this->db->select();
			$select->from("members", array("member_id", "member_name", "headimgurl", "balance_due", "agent_balance", "vip_balance", "delivery_balance"));
			$select->where("cellphone = ?", trim($params['cellphone']));
			
			$row = $this->db->fetchRow($select);
			
			if($row)
			{
				$result["result"] = 1;
				$result["member_id"] = $row["member_id"];
				$result["member_name"] = $row["member_name"];
				$result["headimgurl"] = $row["headimgurl"];
				$result["balance_due"] = $row["balance_due"];
				$result["agent_balance"] = $row["agent_balance"];
				$result["vip_balance"] = $row["vip_balance"];
				$result["delivery_balance"] = $row["delivery_balance"];
			}
		}
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
	
	function checkCartQtyAction()
	{
		$qty = 0;
		
		if($_SESSION['cart']['cart_qty'])
		{
			$qty = $_SESSION['cart']['cart_qty'];
		}
		
		$result = array('qty' => $qty);
		
		echo Zend_Json::encode($result); //back to ajax
    	die;
	}
}